package com.hsq.db137.demos.seriver.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.hsq.db137.demos.entity.Menuinfo;
import com.hsq.db137.demos.entity.Rolemenu;
import com.hsq.db137.demos.mapper.RolemenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsq.db137.demos.entity.Roleinfo;
import com.hsq.db137.demos.mapper.RoleinfoMapper;
import com.hsq.db137.demos.seriver.RoleinfoService;
/**
 * @Author: hsq加油
 * @CreateTime: 2024/12/24-11:48
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class RoleinfoServiceImpl extends ServiceImpl<RoleinfoMapper, Roleinfo> implements RoleinfoService{
@Autowired
RoleinfoMapper roleinfoMapper;
@Autowired
    RolemenuMapper rolemenuMapper;

    /**
     *根据用户ID查询所有的角色ID
     * @param userid 用户ID
     * @return 工具类
     */
    @Override
    public List<Roleinfo> selectRoleByUserid(int userid) {
        return roleinfoMapper.selectRoleByUserid(userid);
    }

    /**
     * 根据角色ID修改角色对应的菜单ID集合

     * @return
     */
    @Override
    public boolean updateRoleMenuId(int roleid, List<Integer> list) {
        //1.根据角色ID删除这个角色的所有菜单
        QueryWrapper<Rolemenu> wrapper = new QueryWrapper<>();
        wrapper.eq("roleid", roleid);
        this.rolemenuMapper.delete(wrapper);
        //2.根据角色ID增加对应的菜单
        for(Integer menuid : list){
            Rolemenu rolemenu = new Rolemenu();
            rolemenu.setRoleid(roleid);
            rolemenu.setMenuid(menuid.toString());
            this.rolemenuMapper.insert(rolemenu);

        }
        return false;
    }

    /**
     * 新增角色
     * @param roleinfo 角色类
     * @return 提示语句
     */
    @Override
    public String insert(Roleinfo roleinfo) {
        return roleinfoMapper.insert(roleinfo)>0?"新增成功":"新增失败";
    }

    /**
     * 根据角色ID查询相对应的菜单集合
     * @param roleid 角色ID
     * @return 集合
     */
    @Override
    public List<Rolemenu> selectRoleMenusByRid(int roleid) {
        return rolemenuMapper.selectList(new QueryWrapper<Rolemenu>().eq("roleId",roleid));
    }

    /**
     * 修改角色
     * @param roleinfo 角色类
     * @return 提示语句
     */
    @Override
    public String updateRoleinfo(Roleinfo roleinfo) {
        return roleinfoMapper.updateById(roleinfo)>0?"修改成功":"修改失败";
    }

    /**
     * 查询全部角色
     * @return 集合
     */
    @Override
    public List<Roleinfo> getRoleinfo() {
        return roleinfoMapper.selectList(null);
    }
}
